#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include "ConfigFile.h"
#include "Manager.h"

using namespace sf;
using namespace std;

GameManager::GameManager()
{

}
void GameManager::LoadCfg(string FileName)
{
    ConfigFile config("udconfig.txt");
    images[Entity::HUMAN]        = loadImage(config.Value("resources", "playerimage"));
    images[Entity::ZOMBIE]       = loadImage(config.Value("resources", "zombieimage"));
    images[Entity::HSTREET]      = loadImage(config.Value("resources", "hstreetimage"));
    images[Entity::VSTREET]      = loadImage(config.Value("resources", "vsteetimage"));
    images[Entity::INTERSECTION] = loadImage(config.Value("resources", "intersectionimage"));
    images[Entity::BUILDING]     = loadImage(config.Value("resources", "bldgimage"));
    // need moar images
}
// 
// name: SetupMap
// 
// Setup the map for use.
void GameManager::SetupMap()
{
	
}
void GameManager::LoadContent()
{

}

void GameManager::LoadContent(vector<string> FileNames) // DEPRECATED
{
    for (int i = 0; i < FileNames.size(); i++)
    {
        images[FileNames[i].substr(
                0, FileNames[i].rfind('.'))] = loadImage(FileNames[i]);
    }
}
void GameManager::Update(float FrameTime)
{
    for (int i = 0; i < entitylist.size(); i++)
    {
		if (entitylist[i]->Alive())
                        entitylist[i]->Update(FrameTime);
    }
}
void GameManager::Draw(RenderTarget& Target)
{
    for (int i = 0; i < entitylist.size(); i++)
    {
		if (entitylist[i]->Alive())
                    entitylist[i].Draw(Target);
    }
}
Image GameManager::loadImage(string FileName)
{
	Image img;
	if (!img.LoadFromFile(FileName))
	{
		cout << "Failed to load image from file: " << FileName << endl;
	}
	return img;
}
void GameManager::AddEntity(Entity::EType Type, Vector2f Position)
{
    switch (Type)
    {
    case Entity::EType::ENEMY:
        entitylist.push_back(new Enemy(images[ZOMBIE_IMAGE], this, Position, PlayerPtr()));
        break;
    case Entity::EType::SCENERY:
        entitylist.push_back(new Scenery(images[WALL_IMAGE], this, Position));
        break;
    case Entity::EType::PLAYER:
        player = new Player(images[PLAYER_IMAGE], this, Position);
        entitylist.push_back(player);
        break;
    default:
        cout << "Entity type not implemented." << endl;
        break;
    }
}
void GameManager::AddEntity(Entity* e)
{
	if (e->Type() == PLAYER) player = dynamic_cast<Player*>(e);
	entitylist.push_back(e);
}

void GameManager::AddEntities(vector<Entity*>& eList)
{
	entitylist.insert(entitylist.end(), eList.begin(), eList.end());
}
vector<Entity *>* GameManager::Entities() { return &entitylist; }

vector<Enemy *> GameManager::Enemies()
{
    vector<Enemy *> enemies;
    for (int i = 0; i < entitylist.size(); i++)
    {
        if (entitylist[i]->Type() == ENEMY)
            enemies.push_back(dynamic_cast<Enemy*>(entitylist[i]));
    }
    return enemies;
}
vector<Scenery*> GameManager::Sceneries()
{
    vector<Scenery*> sceneries;
    for (int i =0; i < entitylist.size(); i++)
    {
        if (entitylist[i]->Type() == SCENERY)
            sceneries.push_back(dynamic_cast<Scenery*>(entitylist[i]));
    }
    return sceneries;
}
const Image* GameManager::GetImage(const string& ImageName)
{
	return &images[ImageName];
}
const Image* GameManager::GetImage(const EType& Type)
{
	switch (Type)
	{
	case SCENERY:
		
	}
}
Player* GameManager::PlayerPtr() { return player; }
void GameManager::FinalizeTiles()
{
    
}
